﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class UserOrderController : UserController
    {
        public string action = string.Empty;
        public int page=1;         //当前页码
        public int totalcount=0;   //OUT数据总数
        public IPaymentService _payment { get; set; }
        public IOrdersService _orders { get; set; }
        public IExpressService _express { get; set; }
        public IActionResult Index()
        {
            action = Request.Query["action"];
            if (string.IsNullOrEmpty(action))
                action = "list";            
            int.TryParse(Request.Query["page"], out page);
            page = page > 0 ? page : 1;
            ViewBag.action = action;
            ViewBag.Page = page;
            var list = _orders.GetList(page, 50, "where user_id=" + userModel.id, "add_time desc,id desc");
            totalcount = _orders.RecordCount("where user_id=" + userModel.id);
            string pageUrl = Utils.CombUrlTxt("/Web/Userorder/Index", "page={0}&act={1}", "__id__",action);
            ViewBag.pagelist = Utils.OutPageList(50, page, totalcount, pageUrl, 8);
            ViewBag.action = action;
            return View(list);
        }
        protected string expressdetail = string.Empty; //物流跟踪信息
        public Orders model;
        public Payment payModel;
        public IActionResult Show(int id)
        {
            if (!_orders.Exists(id))
            {
                return ToError("出错了，您要浏览的页面不存在或已删除！");
            }
            model = _orders.GetModel(id);
            if (model.user_id != userModel.id)
            {
                return ToError("出错了，您所查看的并非自己的订单信息！");
            }
            payModel = _payment.GetModel(model.payment_id);
            if (payModel == null)
            {
                payModel = new Payment();
            }
            //根据订单状态和物流单号跟踪物流信息
            if (model.status > 1 && model.status < 4 && model.express_status == 2 && model.express_no.Trim().Length > 0)
            {
                Express modelt = _express.GetModel(model.express_id);
                Orderconfig orderConfig = Common.XMLConfig.Orderconfig.loadConfig(_env.ContentRootPath);
                if (modelt != null && modelt.express_code.Trim().Length > 0 && orderConfig.kuaidiapi != "")
                {
                    //string apiurl = orderConfig.kuaidiapi + "?id=" + orderConfig.kuaidikey + "&com=" + modelt.express_code + "&nu=" + model.express_no + "&show=" + orderConfig.kuaidishow + "&muti=" + orderConfig.kuaidimuti + "&order=" + orderConfig.kuaidiorder;
                    string detail = KuaiDi100.Check(modelt.express_code, model.express_no, orderConfig.kuaidiid,orderConfig.kuaidikey, orderConfig.kuaidiapi);
                    if (detail != null)
                    {
                        expressdetail = Utils.ToHtml(detail);
                    }
                }
            }
            ViewData["Paymodel"] = payModel;
            ViewBag.ID = id;
            return View(model);
        }
        public IActionResult Edit(int id)
        {
            var order = _orders.GetModel(id);
            if(order.user_id!=userModel.id)
            {
                return Content("非法修改");
            }
            return View(order);
        }
    }
}